#ifndef __DRIVER_KEY_H
#define __DRIVER_KEY_H
#include "py32f0xx_hal.h"
#include <stdint.h>
#include <stdbool.h>

#define KEY_DOWN 0
#define LONG_PRESS_TIME 2000
#define SHORT_PRESS_TIME 500
#define DOUBLE_CLICK_TIME 500 // 间隔小于500ms，认为是连击

#define Key1_GPIO_CLK_ENABLE __HAL_RCC_GPIOA_CLK_ENABLE
#define Key1_GPIO_PORT GPIOA
#define Key1_GPIO_PIN GPIO_PIN_7

#define Key2_GPIO_CLK_ENABLE __HAL_RCC_GPIOA_CLK_ENABLE
#define Key2_GPIO_PORT GPIOA
#define Key2_GPIO_PIN GPIO_PIN_4

#define Key3_GPIO_CLK_ENABLE __HAL_RCC_GPIOA_CLK_ENABLE
#define Key3_GPIO_PORT GPIOA
#define Key3_GPIO_PIN GPIO_PIN_1

//
typedef enum
{
    BUTTON_RELEASED,
    BUTTON_PRESSED,
    BUTTON_SHORT_PRESSED,
    BUTTON_LONG_PRESSED,
    BUTTON_MULTI_PRESSING
} ButtonState;

//
typedef struct
{
    GPIO_TypeDef *GPIOx;
    uint8_t pin;                                       // io??
    bool (*readIO)(GPIO_TypeDef *GPIOx, uint8_t pin); // ??io??
    uint16_t longPressTime;                          // ????ms
    uint16_t shortPressTime;                         // ??ms
    uint16_t multiPressInterval;                     // ??????ms
    uint32_t lastChangeTime;
    uint32_t lastShortPressTime;
    uint8_t pressCount;
    uint8_t id; // ??ID
    ButtonState state;
    void (*onLongPress)(uint16_t duration, uint8_t id); // ??????
    void (*onShortPress)(uint8_t id);                   // ??????
    void (*onMultiPress)(uint8_t count, uint8_t id);    // ??????,???id
} Button;

uint32_t millis(void);
bool readPin(GPIO_TypeDef *GPIOx, uint8_t pin);
void buttonInit(Button buttons[], uint8_t size);
void buttonScan(Button *buttons, uint8_t size);
#endif
